<?php
# FUNCTIONS_MYSQL.PHP
# MySql functions
# Included in every pages

// MySQL functions
class mysql 
{
	// Define Variables
	var $errno;
	var $error;
	var $error_msg;
	var $link;
	
	/*==========================*\
	|| Error Handling Functions ||
	\*==========================*/
	
	// Get Errors
	function getError()
	{
		if(empty($this->error))
		{
			$this->errno = mysql_errno();
			$this->error = mysql_error();
		}
		return $this->error.' (code:'.$this->errno.')';
	}
		
	// Print Error Message
	function printError($msg1,$msg2)
	{
		printf('<html>
				<head>
				<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
				<title>Database Error!</title>
				<style>
				#mysql_error_frame { width:500px; border:1px solid #CC0000; padding:1px}
				.mysql_error { font-size:12px; font-family:Arial; height:20px; padding:1px; text-align:left }
				.title { font-weight:bold; background-color:#CC0000; color:#FFFFFF}
				.desc { font-weight:bold;background-color:#EEEEEE;}
				.detail {font-size:11px; color:#666666;background-color:#FFFFFF;}
				</style>
				</head>
				
				<body>
				<div align=center>
				<div id="mysql_error_frame">
					<div class="mysql_error title">%s</div>
					<div class="mysql_error desc">%s</div>
					<div class="mysql_error detail">%s</div>
				</div>
				</div>
				</body>
				</html>', 'DATABASE ERROR !',$msg1,$msg2);
		exit;
	}
	

  	// PHP Equivalent: mysql_connect
	function connect($host, $user, $pass, $db)
	{
		$this->link = @mysql_connect($host, $user, $pass);
		if(!$this->link)
		{
			$this->errno = 0;
			$this->error = "Connection failed to " . $host . ".";
			$this->error_msg = $this->errno . ": " . $this->error;
			return $this->printError($this->error_msg,0);
		}
		elseif(!@mysql_select_db($db, $this->link))
		{
			$this->errno = mysql_errno();
			$this->error = mysql_error();
			$this->error_msg = $this->printError($this->getError(),0);
			return $this->error_msg;
		}
		else
		{
			return $this->link;
		}
	}
	
	function close()
	{
		mysql_close($this->link);
	}
	
	function query($sql)
	{
		global $query;
		// Count query
		$query[] = $sql;
		
		$query_result = @mysql_query($sql, $this->link);
		if(!$query_result)
		{
			$this->error_msg = $this->printError($this->getError(),$sql);
			return $this->error_msg;
		}
		else
			return $query_result;
	}
	
	function affected_rows()
	{
		return mysql_affected_rows($this->link);
	}
	
	function escape_string($string)
	{
		return mysql_escape_string($string);
	}
	
	function fetch_array($query)
	{
		return mysql_fetch_array($query);
	}
	
	function fetch_field($query, $offset)
	{
		$query = mysql_fetch_field($query, $offset);
		if(!$query)
		{
			$this->errno = 0;
			$this->error = "No information available!";
			$this->error_msg = $this->errno . ": " . $this->error;
			return $this->printError($this->error_msg,0);
		}
		else
		{
			return $query;
		}
	}
	
	function fetch_row($query)
	{
		return mysql_fetch_row($query);
	}
	
	function fetch_assoc($query)
	{
		return mysql_fetch_assoc($query);
	}
	
	function field_name($query, $offset)
	{
		return mysql_field_name($query, $offset);
	}
	
	function free_result($query)
	{
		mysql_free_result($query);
	}
	
	function list_fields($table_name)
	{
		mysql_list_fields($database['name'],$table_name);
	}
	
	
	function insert_id()
	{
		return mysql_insert_id($this->link);
	}
	
	function num_fields($query)
	{
		return mysql_num_fields($query);
	}
	
	function num_rows($query)
	{
		return mysql_num_rows($query);
	}
	
	function real_escape_string($string)
	{
		return mysql_real_escape_string($string, $this->link);
	}
	
	function result($query,$x,$field)
	{
		return @mysql_result($query,$x, $field);
	}
	
	function result_array($query,$x,$string_field)
	{
		$string_array = explode(",",$string_field);
		foreach ($string_array as $string_id=>$string_value)
		{
			$result[$string_value] = $this->result($query,$x,$string_value);
		}
		return $result;
	}
}
?>